package linlin;

/**
 * Created by lingfengsan on 2018/9/12.
 *
 * @author lingfengsan
 */
public class StringProcess {
    public static void main(String[] args) {
        System.out.println(delteBlank(" abc "));
        System.out.println(delteBlank(" abc 123 "));
    }

    public static String delteBlank(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                sb.append(' ');
                while (i < str.length() && str.charAt(i) == ' ') i++;
                i--;
            } else {
                sb.append(str.charAt(i));
            }
        }
        int begin = sb.charAt(0) == ' ' ? 1 : 0;
        int end = sb.charAt(sb.length() - 1) == ' ' ?sb.length()-1:sb.length();
        return sb.substring(begin,end);
    }
}
